// 
// SoapTrace.Net - A soap message monitoring utility. 
// 
// Copyright (C) 2004 Matthew Ward
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
// Matthew Ward (mrward@users.sourceforge.net)
// 

using System;
using System.Diagnostics;
using System.Text;

namespace SoapTrace.Core.Communication
{
	/// <summary>
	/// The header of a soap message.
	/// </summary>
	public class SoapRequestHeader : SoapHeader
	{
		/// <summary>
		/// The Http request method contained in the Http header.
		/// </summary>
		private string m_RequestMethod = String.Empty;

		/// <summary>
		/// The Http request uri contained in the Http header.
		/// </summary>
		private string m_RequestUri = String.Empty;

		/// <summary>
		/// The Http version contained in the Http header.
		/// </summary>
		private string m_HttpVersion = String.Empty;

		/// <summary>
		/// The soap action header field contained in the Http header.
		/// </summary>
		private string m_SoapAction = String.Empty;

		/// <summary>
		/// The name of the soap action field in the Http header.
		/// </summary>
		public const string SoapActionFieldName = "SoapAction";
		
		/// <summary>
		/// Creates a new instance of the <see cref="SoapRequestHeader"/> class.
		/// </summary>
		public SoapRequestHeader( )
		{
		}

		/// <summary>
		/// Gets the Http request method.
		/// </summary>
		public string RequestMethod
		{
			[ DebuggerStepThrough ]
			get
			{
				return m_RequestMethod;
			}
		}

		/// <summary>
		/// Gets the Http request uri.
		/// </summary>
		public string RequestUri
		{
			[ DebuggerStepThrough ]
			get
			{
				return m_RequestUri;
			}
		}

		/// <summary>
		/// Gets the Http version in the Http header.
		/// </summary>
		public string HttpVersion
		{
			[ DebuggerStepThrough ]
			get
			{
				return m_HttpVersion;
			}
		}

		/// <summary>
		/// Gets the Http header's soap action.
		/// </summary>
		public string SoapAction
		{
			[ DebuggerStepThrough ]
			get
			{
				return m_SoapAction;
			}
		}

		/// <summary>
		/// Adds and parses a single byte of the Http header
		/// </summary>
		/// <param name="messageByte">A byte of the Http header.</param>
		/// <returns>A status flag indicating whether the last
		/// byte of the Http header was received.</returns>
		public override bool AddByte( byte messageByte )
		{
			bool HeaderReceived = base.AddByte( messageByte );
			
			if( HeaderReceived )
			{
				ParseRequestLine( base.StartLine );
				GetSoapAction( );
			}

			return HeaderReceived;
		}

		/// <summary>
		/// Parses the request line which has the form 
		/// "Method SP Request-URI SP HTTP-Version CRLF"
		/// </summary>
		/// <param name="requestLine">The Http request line.</param>
		private void ParseRequestLine( string requestLine )
		{
			// Remove CRLF and spaces from end of line.

			requestLine = requestLine.TrimEnd( '\r', '\n', ' ' );

			string[ ] Fields = requestLine.Split( ' ' );

			if( Fields.Length > 2 )
			{
				m_RequestMethod = Fields[ 0 ];
				m_RequestUri = Fields[ 1 ] ;
				m_HttpVersion = Fields[ 2 ];
			}
		}

		/// <summary>
		/// Gets the soap action field's value from the Http header.
		/// </summary>
		private void GetSoapAction( )
		{
			SoapHeaderField Field = base.Fields.Find( SoapRequestHeader.SoapActionFieldName );
			
			if( Field != null )
			{
				m_SoapAction = Field.Value;
			}
		}
	}
}
